import DCLib.RealView
import DCLib.Tool
import DCLib.Config
import DCLib.Map
import DCLib.Unit
import DCLib.Task
import os

if not os.path.exists(DCLib.Config.TMP_DIR):
    print(DCLib.Config.TMP_DIR + " is not exist")
else:
    if not os.path.exists(DCLib.Config.TMP_DIR + "python_script_cache/"):
        os.makedirs(DCLib.Config.TMP_DIR + "python_script_cache/")


def run(task_unit: DCLib.Unit.UnifyTask, input_data_unit: DCLib.Unit.UnifyData, output_data_unit: DCLib.Unit.UnifyData):
    # 头部有一个Task_node，根据头部Task的类型来生成一个对应类型的data
    if task_unit.entrance.task_type == "local_seq_task":
        # 生成一个local_data
        input_data = DCLib.RealView.LocalDataNode(input_data_unit.data_url)
        task_unit.entrance.input_data = input_data
        input_data.task_to_arr.append(task_unit.entrance)
        # 递归执行所有的task
        task_unit.entrance.print_obj()
        task_unit.entrance.run(True)

        # 用task_unit的终点给output_data_unit赋值
        if task_unit.exit.data_type == "local_disk_data":
            output_data_unit.data_url = "file:" + task_unit.exit.local_file_name
        elif task_unit.exit.data_type == "k8s_hdfs_data":
            output_data_unit.data_url = "hdfs:" + task_unit.exit.hdfs_file_dir
        
    else:
        print("DCLib.run:other task type is not supported")
        exit(-1)

